
package com.contractmodule.dao;
import com.contractmodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbContractDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbContract tbContract=null;
    private List<TbContract> tbContractList=null;

    public boolean add(TbContract inTbContract){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Contract(ContractCode,ContractName,ContractType,ContractContent,StartDateSigned,EndDateSigned,ContractState,ContractPrice,CompanyId,CompanyContract,UserId,IsArchive,ContractSignedTime,OurCompanyUser) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbContract.getContractCode());
        pt.setString(2,inTbContract.getContractName());
        pt.setInt(3,inTbContract.getContractType());
        pt.setString(4,inTbContract.getContractContent());
        pt.setString(5,inTbContract.getStartDateSigned());
        pt.setString(6,inTbContract.getEndDateSigned());
        pt.setInt(7,inTbContract.getContractState());
        pt.setDouble(8,new Double(inTbContract.getContractPrice()));
        pt.setInt(9,inTbContract.getCompanyId());
        pt.setString(10,inTbContract.getCompanyContract());
        pt.setInt(11,inTbContract.getUserId());
        pt.setInt(12,inTbContract.getIsArchive());
        pt.setString(13, inTbContract.getContractSignedTime());
        pt.setString(14, inTbContract.getOurCompanyUser());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    public boolean del(int inContractId){
        return UtilDAO.del("Tb_Contract", "ContractId", inContractId);
    }
    public boolean edit(TbContract inTbContract){
        try{
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Contract set ContractName=?,ContractType=?,ContractContent=?,StartDateSigned=?,EndDateSigned=?,ContractState=?,ContractPrice=?,CompanyId=?,CompanyContract=?,UserId=?,IsArchive=?,CreateTime=getdate(),ContractSignedTime=?,OurCompanyUser=? where ContractCode=?";
        pt = conn.prepareStatement(sql);
        pt.setString(1,inTbContract.getContractName());
        pt.setInt(2,inTbContract.getContractType());
        pt.setString(3,inTbContract.getContractContent());
        pt.setString(4,inTbContract.getStartDateSigned());
        pt.setString(5,inTbContract.getEndDateSigned());
        pt.setInt(6,inTbContract.getContractState());
        pt.setDouble(7,new Double(inTbContract.getContractPrice()));
        pt.setInt(8,inTbContract.getCompanyId());
        pt.setString(9,inTbContract.getCompanyContract());
        pt.setInt(10,inTbContract.getUserId());
        pt.setInt(11,inTbContract.getIsArchive());
        pt.setString(12, inTbContract.getContractSignedTime());
        pt.setString(13, inTbContract.getOurCompanyUser());
        pt.setString(14,inTbContract.getContractCode());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    }
    /**
     * 归档
     * @param int ContractCode 合同编号
     * @return
     */
    public boolean archiveByContractCode(String contractCode){
    	try{
    		    conn=ConnectDB.getSqlServerConnection();
    	        sql="update Tb_Contract set IsArchive=1 where ContractCode=?";
    	        pt = conn.prepareStatement(sql);
    	        pt.setString(1, contractCode);
    	        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return flag;
    	 
    }
    public TbContract getTbContractByContractId(int inContractId) {
        tbContract=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Contract where ContractId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inContractId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbContract == null)tbContract = getTbContract(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbContract;
    }
    public TbContract getTbContractByContractCode(String contractCode) {
        tbContract=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Contract where ContractCode=? ";
            pt = conn.prepareStatement(sql);
            pt.setString(1, contractCode);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContract = getTbContract(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbContract;
    }
    public List<TbContract> getTbContractByContractTypeAndIsArchive(int contractType,int isArchive) {
        tbContractList=new ArrayList<TbContract>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Contract where ContractType=? and IsArchive=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1, contractType);
            pt.setInt(2, isArchive);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractList.add(getTbContract(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbContractList;
    }
    public List<TbContract> getTbContractByIsArchive() {
        tbContractList=new ArrayList<TbContract>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Contract where IsArchive=0";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractList.add(getTbContract(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbContractList;
    }
    public List<TbContract> getTbContractAll() {
        tbContractList=new ArrayList<TbContract>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Contract";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractList.add(getTbContract(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbContractList;
    }
    public List<TbContract> getTbContractAll(VskPage vp) {
        tbContractList=new ArrayList<TbContract>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Contract";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Contract", "ContractId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbContractList.add(getTbContract(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
                        ConnectDB.closeSqlConnection();        }
        return tbContractList;
    }
    public TbContract getTbContract(ResultSet rs) throws Exception{
        TbContract tbContract = new TbContract();
        tbContract.setContractId(rs.getInt("ContractId"));
        tbContract.setContractCode(rs.getString("ContractCode"));
        tbContract.setContractName(rs.getString("ContractName"));
        tbContract.setContractType(rs.getInt("ContractType"));
        tbContract.setContractContent(rs.getString("ContractContent"));
        tbContract.setStartDateSigned(rs.getString("StartDateSigned"));
        tbContract.setEndDateSigned(rs.getString("EndDateSigned"));
        tbContract.setContractState(rs.getInt("ContractState"));
        tbContract.setContractPrice(rs.getString("ContractPrice"));
        tbContract.setCompanyId(rs.getInt("CompanyId"));
        tbContract.setCompanyContract(rs.getString("CompanyContract"));
        tbContract.setUserId(rs.getInt("UserId"));
        tbContract.setIsArchive(rs.getInt("IsArchive"));
        tbContract.setContractSignedTime(rs.getString("ContractSignedTime"));
        tbContract.setOurCompanyUser(rs.getString("OurCompanyUser"));
        tbContract.setCreateTime(rs.getString("CreateTime"));
        return tbContract;
    }
    public static void main(String[] args) {
		System.out.println(new TbContractDAO().archiveByContractCode("20110516N01"));
	}
}
